###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces figures for ideological placements by levels of sophistication, H,M,L
########################################################

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R

rm(list=ls()[which(ls()!='dirs')])
library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/vdl.cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

load('data/vdl.data_matrix_scored.Rdata')
candidate_matrix=data_matrix
#read.csv("csv/candidate_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

# knowledge/sophistication/ideologue indices
load('data/sophistication_indices.Rdata')

L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

candidate_matrix$sophisticated_level=NA
for(i in 1:length(L_ix2)){
	iq=which(L_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='L'
}
for(i in 1:length(M_ix2)){
	iq=which(M_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='M'
}
for(i in 1:length(H_ix2)){
	iq=which(H_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='H'
}

################################################################
# consistency pooling dems and reps
################################################################

candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


ds=quantile(candidate_matrix$libcon[which(candidate_matrix$pid==-1)],pr=c(.75,.5))
rs=quantile(candidate_matrix$libcon[which(candidate_matrix$pid== 1)],pr=c(.25,.5))

candidate_matrix$crossp25=NA
candidate_matrix$crossp50=NA

candidate_matrix$crossp25[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[1])
candidate_matrix$crossp50[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[2])

candidate_matrix$crossp25[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[1])
candidate_matrix$crossp50[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[2])



###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),]
table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),]

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2])
#H         M         L
#0.4748879 0.3721973 0.1529148

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1])
#H         M         L
#0.2746807 0.3492111 0.3761082

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2])
#H         M         L
#0.4033544 0.3656860 0.2309596

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1])
#H         M         L
#0.2705793 0.3475610 0.3818598

############################################################
############################################################
###Stratify by cross 50 == 1 + validated general


ideo_extreme <- subset(candidate_matrix,candidate_matrix$sophisticated_level=='H' &  (candidate_matrix$conjoints==1|candidate_matrix$conjoints==2|candidate_matrix$conjoints==5|candidate_matrix$conjoints==6))

#Standardizing the direction across 1,2,5,6 JAHconjoints w/ Lib = 0, Cons = 1
ideo_extreme$dv_libcon <- ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==0,0,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==0,0,NA))))))))

ideo_extreme$pty <- ifelse(ideo_extreme$conjoints==1 | ideo_extreme$conjoints==5,0,1)

#####Global Results

attach(ideo_extreme)
global_ideo <- lm(dv_libcon~
pty+g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

global_ideo_clse <- clse.f(ideo_extreme,global_ideo,respondent)
detach(ideo_extreme)

results_matrix <- cbind(global_ideo_clse[,1:2])
colnames(results_matrix) <- c("estimate","se")
results_matrix <- results_matrix[3:59,]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,2))
re_white <- c("re_white",rep(0,2))
r_none <- c("r_none",rep(0,2))
o_attorney <- c("o_attorney",rep(0,2))
p_decent <- c("p_decent",rep(0,2))
e_newspapers <- c("e_newspapers",rep(0,2))
rec_help <- c("rec_help",rep(0,2))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,2))
i_freetrade <- c("i1_freetrade",rep(0,2))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='est')

core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

#write.csv(core_for_ggplot,"data/core_for_ggplot_global.csv")

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("data/core_for_ggplot_global.csv")

attach(core_for_ggplot)
labels=core_for_ggplot$labels

minci <- (estimate - (1.96*se))
maxci <- (estimate + (1.96*se))

core_for_ggplot$PID=paste('All (N=',length(unique(ideo_extreme$respondent)),')',sep='')


pdf("appendix/figures/libcon_primary_H_sophisticated.pdf", width=9, height=14, pointsize=12)
ggplot(core_for_ggplot,
    aes(x=iv_order, y=estimate, group=PID)) +
    geom_line(y=0,size=0.5,color="gray80") +
    scale_x_discrete(limits=rev(iv_order)) +
		#facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.26,0.26) +
		#theme_bw() +
		#theme(axis.text.y = element_text(hjust=0,color="gray40")) +
		theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
	theme(legend.text = element_text(size = 10)) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Candidate is More Conservative)")
dev.off()

detach(core_for_ggplot)
core_for_ggplotH=core_for_ggplot
###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

# validated primary/general voters

rm(list=ls()[which(ls()!='core_for_ggplotH' & ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/vdl.cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)


load('data/vdl.data_matrix_scored.Rdata')
candidate_matrix=data_matrix
#read.csv("csv/candidate_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

# knowledge/sophistication/ideologue indices
load('data/sophistication_indices.Rdata')

L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

candidate_matrix$sophisticated_level=NA
for(i in 1:length(L_ix2)){
	iq=which(L_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='L'
}
for(i in 1:length(M_ix2)){
	iq=which(M_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='M'
}
for(i in 1:length(H_ix2)){
	iq=which(H_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='H'
}

################################################################
# consistency pooling dems and reps
################################################################

candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


ds=quantile(candidate_matrix$libcon[which(candidate_matrix$pid==-1)],pr=c(.75,.5))
rs=quantile(candidate_matrix$libcon[which(candidate_matrix$pid== 1)],pr=c(.25,.5))

candidate_matrix$crossp25=NA
candidate_matrix$crossp50=NA

candidate_matrix$crossp25[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[1])
candidate_matrix$crossp50[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[2])

candidate_matrix$crossp25[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[1])
candidate_matrix$crossp50[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[2])



###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),]
table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),]

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2])
#H         M         L
#0.4748879 0.3721973 0.1529148

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1])
#H         M         L
#0.2746807 0.3492111 0.3761082

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2])
#H         M         L
#0.4033544 0.3656860 0.2309596

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1])
#H         M         L
#0.2705793 0.3475610 0.3818598

############################################################
############################################################
###Stratify by cross 50 == 1 + validated general

ideo_extreme <- subset(candidate_matrix,candidate_matrix$sophisticated_level!='H'  &  (candidate_matrix$conjoints==1|candidate_matrix$conjoints==2|candidate_matrix$conjoints==5|candidate_matrix$conjoints==6))

#Standardizing the direction across 1,2,5,6 JAHconjoints w/ Lib = 0, Cons = 1
ideo_extreme$dv_libcon <- ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==0,0,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==0,0,NA))))))))

ideo_extreme$pty <- ifelse(ideo_extreme$conjoints==1 | ideo_extreme$conjoints==5,0,1)

#####Global Results

attach(ideo_extreme)
global_ideo <- lm(dv_libcon~
pty+g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

global_ideo_clse <- clse.f(ideo_extreme,global_ideo,respondent)
detach(ideo_extreme)

results_matrix <- cbind(global_ideo_clse[,1:2])
colnames(results_matrix) <- c("estimate","se")
results_matrix <- results_matrix[3:59,]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,2))
re_white <- c("re_white",rep(0,2))
r_none <- c("r_none",rep(0,2))
o_attorney <- c("o_attorney",rep(0,2))
p_decent <- c("p_decent",rep(0,2))
e_newspapers <- c("e_newspapers",rep(0,2))
rec_help <- c("rec_help",rep(0,2))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,2))
i_freetrade <- c("i1_freetrade",rep(0,2))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='est')

core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

#write.csv(core_for_ggplot,"data/core_for_ggplot_global.csv")

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("data/core_for_ggplot_global.csv")

attach(core_for_ggplot)
labels=core_for_ggplot$labels

minci <- (estimate - (1.96*se))
maxci <- (estimate + (1.96*se))

core_for_ggplot$PID=paste('All (N=',length(unique(ideo_extreme$respondent)),')',sep='')


pdf("appendix/figures/libcon_primary_notH_sophisticated.pdf", width=9, height=14, pointsize=12)
ggplot(core_for_ggplot,
    aes(x=iv_order, y=estimate, group=PID)) +
    geom_line(y=0,size=0.5,color="gray80") +
    scale_x_discrete(limits=rev(iv_order)) +
		#facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.26,0.26) +
		#theme_bw() +
		#theme(axis.text.y = element_text(hjust=0,color="gray40")) +
		theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
	theme(legend.text = element_text(size = 10)) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Candidate is More Conservative)")
dev.off()

detach(core_for_ggplot)


###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

# validated primary/general voters

rm(list=ls()[which(ls()!='core_for_ggplotH' & ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/vdl.cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)


load('data/vdl.data_matrix_scored.Rdata')
candidate_matrix=data_matrix
#read.csv("csv/candidate_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

# knowledge/sophistication/ideologue indices
load('data/sophistication_indices.Rdata')

L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

candidate_matrix$sophisticated_level=NA
for(i in 1:length(L_ix2)){
	iq=which(L_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='L'
}
for(i in 1:length(M_ix2)){
	iq=which(M_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='M'
}
for(i in 1:length(H_ix2)){
	iq=which(H_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='H'
}

################################################################
# consistency pooling dems and reps
################################################################

candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


ds=quantile(candidate_matrix$libcon[which(candidate_matrix$pid==-1)],pr=c(.75,.5))
rs=quantile(candidate_matrix$libcon[which(candidate_matrix$pid== 1)],pr=c(.25,.5))

candidate_matrix$crossp25=NA
candidate_matrix$crossp50=NA

candidate_matrix$crossp25[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[1])
candidate_matrix$crossp50[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[2])

candidate_matrix$crossp25[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[1])
candidate_matrix$crossp50[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[2])



###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),]
table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),]

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2])
#H         M         L
#0.4748879 0.3721973 0.1529148

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1])
#H         M         L
#0.2746807 0.3492111 0.3761082

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2])
#H         M         L
#0.4033544 0.3656860 0.2309596

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1])
#H         M         L
#0.2705793 0.3475610 0.3818598

############################################################
############################################################
###Stratify by cross 50 == 1 + validated general


ideo_extreme <- subset(candidate_matrix, candidate_matrix$sophisticated_level=='M' &  (candidate_matrix$conjoints==1|candidate_matrix$conjoints==2|candidate_matrix$conjoints==5|candidate_matrix$conjoints==6))

#Standardizing the direction across 1,2,5,6 JAHconjoints w/ Lib = 0, Cons = 1
ideo_extreme$dv_libcon <- ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==0,0,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==0,0,NA))))))))

ideo_extreme$pty <- ifelse(ideo_extreme$conjoints==1 | ideo_extreme$conjoints==5,0,1)

#####Global Results

attach(ideo_extreme)
global_ideo <- lm(dv_libcon~
pty+g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

global_ideo_clse <- clse.f(ideo_extreme,global_ideo,respondent)
detach(ideo_extreme)

results_matrix <- cbind(global_ideo_clse[,1:2])
colnames(results_matrix) <- c("estimate","se")
results_matrix <- results_matrix[3:59,]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,2))
re_white <- c("re_white",rep(0,2))
r_none <- c("r_none",rep(0,2))
o_attorney <- c("o_attorney",rep(0,2))
p_decent <- c("p_decent",rep(0,2))
e_newspapers <- c("e_newspapers",rep(0,2))
rec_help <- c("rec_help",rep(0,2))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,2))
i_freetrade <- c("i1_freetrade",rep(0,2))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='est')

core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

#write.csv(core_for_ggplot,"data/core_for_ggplot_global.csv")

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("data/core_for_ggplot_global.csv")

attach(core_for_ggplot)
labels=core_for_ggplot$labels

minci <- (estimate - (1.96*se))
maxci <- (estimate + (1.96*se))

core_for_ggplot$PID=paste('All (N=',length(unique(ideo_extreme$respondent)),')',sep='')


pdf("appendix/figures/libcon_primary_M_sophisticated.pdf", width=9, height=14, pointsize=12)
ggplot(core_for_ggplot,
    aes(x=iv_order, y=estimate, group=PID)) +
    geom_line(y=0,size=0.5,color="gray80") +
    scale_x_discrete(limits=rev(iv_order)) +
		#facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.26,0.26) +
		#theme_bw() +
		#theme(axis.text.y = element_text(hjust=0,color="gray40")) +
		theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
	theme(legend.text = element_text(size = 10)) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Candidate is More Conservative)")
dev.off()

detach(core_for_ggplot)
core_for_ggplotM=core_for_ggplot
###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

# validated primary/general voters

rm(list=ls()[which(ls()!='core_for_ggplotH' & ls()!='core_for_ggplotM' & ls()!='dirs')])

###########################################
#  Ideology and Electability in Primary Voting Behavior
###########################################
#  - code by S. Goggin (Aug 10, 2017)
#  - edited by J. Henderson (Aug 16, 2017)

# validated primary/general voters


library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/vdl.cces_stacked_unmatched.Rdata")


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)


load('data/vdl.data_matrix_scored.Rdata')
candidate_matrix=data_matrix
#read.csv("csv/candidate_matrix_scored.csv",header=T,stringsAsFactors=F)[,-c(1)]

# knowledge/sophistication/ideologue indices
load('data/sophistication_indices.Rdata')

L_ix2=indices$resp_id[indices$"L_ix2"]
M_ix2=indices$resp_id[indices$"M_ix2"]
H_ix2=indices$resp_id[indices$"H_ix2"]

candidate_matrix$sophisticated_level=NA
for(i in 1:length(L_ix2)){
	iq=which(L_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='L'
}
for(i in 1:length(M_ix2)){
	iq=which(M_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='M'
}
for(i in 1:length(H_ix2)){
	iq=which(H_ix2[i]==candidate_matrix$respondent)
	candidate_matrix$sophisticated_level[iq]='H'
}

################################################################
# consistency pooling dems and reps
################################################################

candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


ds=quantile(candidate_matrix$libcon[which(candidate_matrix$pid==-1)],pr=c(.75,.5))
rs=quantile(candidate_matrix$libcon[which(candidate_matrix$pid== 1)],pr=c(.25,.5))

candidate_matrix$crossp25=NA
candidate_matrix$crossp50=NA

candidate_matrix$crossp25[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[1])
candidate_matrix$crossp50[which(candidate_matrix$pid==-1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid==-1)]>ds[2])

candidate_matrix$crossp25[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[1])
candidate_matrix$crossp50[which(candidate_matrix$pid== 1)]=as.numeric(candidate_matrix$libcon[which(candidate_matrix$pid== 1)]<rs[2])



###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

############################################################

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),]
table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),]

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),2])
#H         M         L
#0.4748879 0.3721973 0.1529148

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_primary)[c(1,3,2),1])
#H         M         L
#0.2746807 0.3492111 0.3761082

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),2])
#H         M         L
#0.4033544 0.3656860 0.2309596

table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1]/sum(table(candidate_matrix$sophisticated_level,candidate_matrix$valid_general)[c(1,3,2),1])
#H         M         L
#0.2705793 0.3475610 0.3818598

############################################################
############################################################
###Stratify by cross 50 == 1 + validated general

ideo_extreme <- subset(candidate_matrix,candidate_matrix$sophisticated_level=='L' & (candidate_matrix$conjoints==1|candidate_matrix$conjoints==2|candidate_matrix$conjoints==5|candidate_matrix$conjoints==6))

#Standardizing the direction across 1,2,5,6 JAHconjoints w/ Lib = 0, Cons = 1
ideo_extreme$dv_libcon <- ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==1& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==1,0,ifelse(
ideo_extreme$conjoints==5& ideo_extreme$dv_choice==0,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==2& ideo_extreme$dv_choice==0,0,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==1,1,ifelse(
ideo_extreme$conjoints==6& ideo_extreme$dv_choice==0,0,NA))))))))

ideo_extreme$pty <- ifelse(ideo_extreme$conjoints==1 | ideo_extreme$conjoints==5,0,1)

#####Global Results

attach(ideo_extreme)
global_ideo <- lm(dv_libcon~
pty+g_female+
re_black+re_hispanic+
r_catholic+r_evangelical+r_protestant+
o_ceo+o_citycouncil+o_factoryforeman+o_farmer+o_usarmymajor+o_politicalstaffer+o_smallbizowner+o_stateleg+o_teacher+
p_compassionate+p_empathetic+p_inspiring+p_intelligent+p_knowledgeable+p_moral+p_strongleader+
e_business+e_christian+e_civilrights+e_energy+e_environment+e_guncontrol+e_gunrights+e_laborunions+e_reproductive+e_taxreform+e_teaparty+e_veterans+
rec_refuse+rec_secure+rec_stand+rec_work+
i_raisetaxes+i_cuttaxes+i_lgbt+i_marriage+i_drilling+i_need+i_govabuse+i_righttochoose+i_gunrights+i_unfairtrade+i_unbornlives+i_citizenship+i_reducemilitary+i_policing+i_co2emissions+i_bordersecurity+i_guncontrol+i_strengthenmilitary+i_criminals,
weights = 1/wt)

global_ideo_clse <- clse.f(ideo_extreme,global_ideo,respondent)
detach(ideo_extreme)

results_matrix <- cbind(global_ideo_clse[,1:2])
colnames(results_matrix) <- c("estimate","se")
results_matrix <- results_matrix[3:59,]
results_matrix_withnames <- cbind(var=rownames(results_matrix),results_matrix)
rownames(results_matrix_withnames) <- seq(1,57,by=1)

##Now inserting rows for the omitted levels

#omitted: male, white, no religion, attorney, decent, newspaper endorsements, record = helping constituents, raising taxes for both issues

g_male <- c("g_male",rep(0,2))
re_white <- c("re_white",rep(0,2))
r_none <- c("r_none",rep(0,2))
o_attorney <- c("o_attorney",rep(0,2))
p_decent <- c("p_decent",rep(0,2))
e_newspapers <- c("e_newspapers",rep(0,2))
rec_help <- c("rec_help",rep(0,2))
#i_raisetaxes <- c("i1_raisetaxes",rep(0,2))
i_freetrade <- c("i1_freetrade",rep(0,2))

full_matrix <- rbind(
g_male,
results_matrix_withnames[1,],
re_white,
results_matrix_withnames[2:3,],
r_none,
results_matrix_withnames[4:6,],
o_attorney,
results_matrix_withnames[7:15,],
p_decent,
results_matrix_withnames[16:22,],
e_newspapers,
results_matrix_withnames[23:34,],
rec_help,
results_matrix_withnames[35:38,],
i_freetrade,
results_matrix_withnames[39:57,]
)

rownames(full_matrix) <- full_matrix[,1]
full_matrix <- full_matrix[,2:ncol(full_matrix)]
full_matrix_clean <- apply(full_matrix,2,as.numeric)
rownames(full_matrix_clean) <- rownames(full_matrix)

labels <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

core_for_ggplot <- data.frame(labels=labels,full_matrix_clean)
core_for_ggplot$iv_order <- factor(core_for_ggplot$labels, as.character(core_for_ggplot$labels))

omits=c("Gender - Male",
"Race - White",
"Religion - None",
"Occupation - Attorney",
"Personality - Decent",
"Endorsements - Major area newspapers",
"Record - Help my constituents get the benefits they deserve",
"Issue - Promote expanding free trade agreements")

label.groups=str_sub(labels,1,str_locate(labels,pattern='-')[,1]-2)
o.column=grep(names(core_for_ggplot),pattern='est')

core_for_ggplot=lableOrder(xmat=core_for_ggplot,labels,label.groups,omits,o.column)

#write.csv(core_for_ggplot,"data/core_for_ggplot_global.csv")

#Doing the within-factor sorting in shitty hack
#core_for_ggplot <- read.csv("data/core_for_ggplot_global.csv")

attach(core_for_ggplot)
labels=core_for_ggplot$labels

minci <- (estimate - (1.96*se))
maxci <- (estimate + (1.96*se))

core_for_ggplot$PID=paste('All (N=',length(unique(ideo_extreme$respondent)),')',sep='')


pdf("appendix/figures/libcon_primary_L_sophisticated.pdf", width=9, height=14, pointsize=12)
ggplot(core_for_ggplot,
    aes(x=iv_order, y=estimate, group=PID)) +
    geom_line(y=0,size=0.5,color="gray80") +
    scale_x_discrete(limits=rev(iv_order)) +
		#facet_wrap(~PID,ncol=2) +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci)) +
    geom_point(shape=21, size=2, fill="black") +
    ylim(-0.26,0.26) +
		#theme_bw() +
		#theme(axis.text.y = element_text(hjust=0,color="gray40")) +
		theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
	theme(legend.text = element_text(size = 10)) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=25.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=38.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=46.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=56.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=60.5,size=0.5,linetype="dashed") +
	geom_vline(xintercept=63.5,size=0.5,linetype="dashed") +
    coord_flip() +
    labs(x = "Experimentally Manipulated Variable", y = "Change Pr(Candidate is More Conservative)")
dev.off()

detach(core_for_ggplot)
core_for_ggplotL=core_for_ggplot


# combine them here
ggplot_stackedH=core_for_ggplotH
ggplot_stackedM=core_for_ggplotM
ggplot_stackedL=core_for_ggplotL

ggplot_stackedH$Consistency=paste('I. High Sophisticated',ggplot_stackedH$PID,sep=' ')
ggplot_stackedM$Consistency=paste('II. Med Sophisticated',ggplot_stackedM$PID,sep=' ')
ggplot_stackedL$Consistency=paste('III. Low Sophisticated',ggplot_stackedL$PID,sep=' ')

#iq=grepl(ggplot_stackedH$labels,pattern='Gender') | grepl(ggplot_stackedH$labels,pattern='Race') | grepl(ggplot_stackedH$labels,pattern='Religion') |
#grepl(ggplot_stackedH$labels,pattern='Endorsements') | grepl(ggplot_stackedH$labels,pattern='Issue')

# merge by PID
if(length(which(objects()=='ggplot_stacked'))>0){
	detach(ggplot_stacked)
}

#ggplot_stacked=rbind(ggplot_stackedH[iq,],ggplot_stackedM[iq,],ggplot_stackedL[iq,])
ggplot_stacked=rbind(ggplot_stackedH,ggplot_stackedM,ggplot_stackedL)
ggplot_stacked$labels=as.character(ggplot_stacked$labels)
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='[N()0-9= ]',replace='')

labels=ggplot_stacked$labels=as.factor(ggplot_stacked$labels)
labels=unique(labels)
pd <- position_dodge(.5)
ggplot_stacked$minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
ggplot_stacked$maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))


attach(ggplot_stacked)

#n1D=length(unique(vote$respondent[which(vote$invoted=='invote' & vote$pid3==-1 & vote$crossp50==1)])) # inconsistent
#n0D=length(unique(vote$respondent[which(vote$invoted=='invote' & vote$pid3==-1 & vote$crossp50==0)])) # consistent
#n1R=length(unique(vote$respondent[which(vote$invoted=='invote' & vote$pid3==1 & vote$crossp50==1)]))
#n0R=length(unique(vote$respondent[which(vote$invoted=='invote' & vote$pid3==1 & vote$crossp50==0)]))

#ggplot_stacked$Consistency=gsub(ggplot_stacked$Consistency,pattern='Inconsistent Democrat',replace=paste(sep='','Inconsistent Democrat (N=',n1D,')'))
#ggplot_stacked$Consistency=gsub(ggplot_stacked$Consistency,pattern='Consistent Democrat',replace=paste(sep='','Consistent Democrat (N=',n0D,')'))
#ggplot_stacked$Consistency=gsub(ggplot_stacked$Consistency,pattern='Inconsistent Republican',replace=paste(sep='','Inconsistent Republican (N=',n1R,')'))
#ggplot_stacked$Consistency=gsub(ggplot_stacked$Consistency,pattern='Consistent Republican',replace=paste(sep='','Consistent Republican (N=',n0R,')'))


pdf("appendix/figures/libcon_primary_H_M_L_sophisticated_short.pdf",  width=9, height=14, pointsize=12)
#pdf("figures/choice_primary_twoparty_crossp50_combo_short.pdf", width=10, height=10, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=Consistency)) +
    scale_color_manual(values = c("#191970","steelblue3","lightsteelblue3")) +
    scale_x_discrete(limits=rev(labels)) +
		facet_wrap(~PID,ncol=2) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.34,0.46) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 		theme(legend.text = element_text(size = 10)) +
		#geom_vline(xintercept=1.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=33.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=37.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=40.5,size=0.5,linetype="dashed") +
		coord_flip() +
		labs(x = "Experimentally Manipulated Variable", y = "Difference in Difference in Pr(Vote Choice)")
dev.off()


if(length(which(objects()=='ggplot_stacked'))>0){
	detach(ggplot_stacked)
}

ggplot_stacked=rbind(ggplot_stackedH,ggplot_stackedM)
ggplot_stacked$labels=as.character(ggplot_stacked$labels)
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='[N()0-9= ]',replace='')

labels=ggplot_stacked$labels=as.factor(ggplot_stacked$labels)
labels=unique(labels)
pd <- position_dodge(.5)
ggplot_stacked$minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
ggplot_stacked$maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))


attach(ggplot_stacked)


pdf("appendix/figures/choice_primary_H_M_sophisticated_short.pdf", width=10, height=10, pointsize=12)
#pdf("figures/choice_primary_twoparty_crossp50_combo_short.pdf", width=10, height=10, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=Consistency)) +
    scale_color_manual(values = c("#191970","lightsteelblue3")) +
    scale_x_discrete(limits=rev(labels)) +
		facet_wrap(~PID,ncol=2) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.34,0.46) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 		theme(legend.text = element_text(size = 10)) +
		#geom_vline(xintercept=1.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=33.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=37.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=40.5,size=0.5,linetype="dashed") +
		coord_flip() +
		labs(x = "Experimentally Manipulated Variable", y = "Difference in Difference in Pr(Vote Choice)")
dev.off()

if(length(which(objects()=='ggplot_stacked'))>0){
	detach(ggplot_stacked)
}

ggplot_stacked=rbind(ggplot_stackedH,ggplot_stackedL)
ggplot_stacked$labels=as.character(ggplot_stacked$labels)
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='[N()0-9= ]',replace='')

labels=ggplot_stacked$labels=as.factor(ggplot_stacked$labels)
labels=unique(labels)
pd <- position_dodge(.5)
ggplot_stacked$minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
ggplot_stacked$maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))


attach(ggplot_stacked)


pdf("appendix/figures/choice_primary_H_L_sophisticated_short.pdf", width=10, height=10, pointsize=12)
#pdf("figures/choice_primary_twoparty_crossp50_combo_short.pdf", width=10, height=10, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=Consistency)) +
    scale_color_manual(values = c("#191970","lightsteelblue3")) +
    scale_x_discrete(limits=rev(labels)) +
		facet_wrap(~PID,ncol=2) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.34,0.46) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 		theme(legend.text = element_text(size = 10)) +
		#geom_vline(xintercept=1.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=33.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=37.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=40.5,size=0.5,linetype="dashed") +
		coord_flip() +
		labs(x = "Experimentally Manipulated Variable", y = "Difference in Difference in Pr(Vote Choice)")
dev.off()


if(length(which(objects()=='ggplot_stacked'))>0){
	detach(ggplot_stacked)
}

ggplot_stacked=rbind(ggplot_stackedM,ggplot_stackedL)
ggplot_stacked$labels=as.character(ggplot_stacked$labels)
ggplot_stacked$PID=gsub(ggplot_stacked$PID,pattern='[N()0-9= ]',replace='')

labels=ggplot_stacked$labels=as.factor(ggplot_stacked$labels)
labels=unique(labels)
pd <- position_dodge(.5)
ggplot_stacked$minci <- (ggplot_stacked$estimate - (1.96*ggplot_stacked$se))
ggplot_stacked$maxci <- (ggplot_stacked$estimate + (1.96*ggplot_stacked$se))


attach(ggplot_stacked)


pdf("appendix/figures/choice_primary_M_L_sophisticated_short.pdf", width=10, height=10, pointsize=12)
#pdf("figures/choice_primary_twoparty_crossp50_combo_short.pdf", width=10, height=10, pointsize=12)
ggplot(ggplot_stacked,
    aes(x=labels, y=estimate, colour=Consistency)) +
    scale_color_manual(values = c("#191970","lightsteelblue3")) +
    scale_x_discrete(limits=rev(labels)) +
		facet_wrap(~PID,ncol=2) +
    geom_hline(yintercept=0,size=0.5,color="gray80") +
    geom_errorbar(width=.1, aes(ymin=minci, ymax=maxci),position=pd) +
    geom_point(shape=21, size=2,position=pd) +
    ylim(-0.34,0.46) +
    theme_bw() +
    theme(axis.text.y = element_text(hjust=0,color="gray40")) +
    theme(legend.position=c(-0.3,0.935),legend.title=element_text()) +
 		theme(legend.text = element_text(size = 10)) +
		#geom_vline(xintercept=1.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=20.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=33.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=37.5,size=0.5,linetype="dashed") +
		geom_vline(xintercept=40.5,size=0.5,linetype="dashed") +
		coord_flip() +
		labs(x = "Experimentally Manipulated Variable", y = "Difference in Difference in Pr(Vote Choice)")
dev.off()

#END
